import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/foundation.dart';
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:junyeyishu/lottery/QGLogOutWidget.dart';
import 'package:junyeyishu/route.dart';
import 'package:junyeyishu/utils/ScreenFit.dart';
import 'package:junyeyishu/widgets/QGNavigationBar.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';
import 'package:svgaplayer_flutter/svgaplayer_flutter.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

//  huode
class _SettingPageState extends State<SettingPage> with SingleTickerProviderStateMixin{
  
  SVGAAnimationController? animationController;

   bool isLoading = true;
  Color backgroundColor = Colors.transparent;
  bool allowOverflow = true;
  // Canvaskit need FilterQuality.high
  FilterQuality filterQuality = kIsWeb ? FilterQuality.high : FilterQuality.low;
  BoxFit fit = BoxFit.contain;
late double containerWidth;
  late double containerHeight;
  bool hideOptions = false;
  @override
  void initState() {
    super.initState();
   animationController = SVGAAnimationController(vsync: this);
   _loadAnimation();
  }

  void _loadAnimation() async {
    // FIXME: may throw error on loading
    final videoItem = await _loadVideoItem('images/dong.svga');
   animationController?.videoItem = videoItem;
    _playAnimation();
  }

  void _playAnimation() {
        animationController?.addListener(() {
          if (animationController?.isCompleted== true) {
            print(66666);
               showDialog(
                    context: context,
                    barrierDismissible: false,
                    builder: (BuildContext context) {
                      return QGLogOut();
            });
          }
        });
  }



  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    containerWidth = math.min(350, MediaQuery.of(context).size.width);
    containerHeight = math.min(350, MediaQuery.of(context).size.height);
  }

  @override
  void dispose() {
    animationController?.dispose();
    animationController = null;
    super.dispose();
  }


  onload() {}

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //  appBar: AppBar(
      //     backgroundColor: Color(0xff333333),
      //     leading: IconButton(
      //     icon: QGImage(asset: "nav_back_white",width: 20.rpx), // 自定义图标
      //     onPressed: () => Navigator.pop(context), // 手动实现返回
      //   ),
      //   ),
        body: EasyRefresh(
      child: NotificationListener(
          // onNotification: onNotification,
          child: Stack(
        children: [
          SingleChildScrollView(
            child: Column(
              children: [
                Stack(
                  children: [
                    QGImage(
                      asset: "choubg",
                      width: 375.rpx,
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 120.rpx),
                      child: QGImage(
                        asset: "jigui",
                        width: 375.rpx,
                      ),
                    ),
                    Positioned(
                      left: 0,
                      right: 0,
                      bottom: 495.rpx,
                      child: Text(
                        '活动时间：03-01 17:00 至 03-07 18:00',
                        style:
                            TextStyle(fontSize: 14, color: Color(0XFFFFFFFF)),
                        textAlign: TextAlign.center,
                      ),
                    ),
                    Positioned(
                      left: 0,
                      right: 0,
                      bottom: 180.rpx,
                      child:Container(
                        height: 300,
                        child:SVGAImage(
                          animationController!,
                          fit: fit,
                          clearsAfterStop: false,
                          allowDrawingOverflow: allowOverflow,
                          filterQuality: filterQuality,
                          preferredSize: Size(containerWidth, containerHeight),
                        )
                      )
                    ),

                    Positioned(
                      left: 0,
                      right: 0,
                      bottom:180.rpx,
                      child: Text(
                        '还可抽奖次数：2',
                        style:
                            TextStyle(fontSize: 12, color: Color(0XFFFFFFFF)),
                        textAlign: TextAlign.center,
                      ),
                    ),

                    Positioned(
                      left: 0,
                      right: 0,
                      bottom: 50.rpx,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          GestureDetector(
                            onTap: (){
                              animationController?.reset();
                              animationController?.forward();
                            },
                            child: QGImage(
                            asset: "yici",
                            width: 115.rpx,
                          ),
                          )
                          ,
                          SizedBox(
                            width: 3.rpx,
                          ),
                          GestureDetector(
                            onTap: (){
                               
                            },
                            child:QGImage(
                            asset: "wuci",
                            width: 115.rpx,
                          ) ,
                          )
                          ,
                          SizedBox(
                            width: 10.rpx,
                          ),
                          Container(
                            margin: EdgeInsets.only(bottom: 30.rpx),
                            child: QGImage(
                              asset: "yaogan",
                              width: 85.rpx,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                Stack(children: [
                  Container(
                      child: QGImage(
                        asset: "dibu",
                        width: 375.rpx,
                      ),
                    ),
                  Positioned(
                    top: 20,
                    right: 20,
                    left: 20,
                    child: 
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 15.rpx,horizontal: 13.rpx),
                      decoration: BoxDecoration(
                       color: Color(0XFF232A32),
                        borderRadius: BorderRadius.circular(10.rpx)
                      ),
                      child: Column(
                        children: [
                          Text('活动规则',style: TextStyle(fontSize: 16,color: Color(0XFFFFFFFF))),
                          SizedBox(height: 15.rpx,),
                          Container(
                            height: 270.rpx,
                            child: ListView(
                              children: [
                                Text('活动奖品如下活动奖品如下活动奖品如下活动奖品如下活动品如下活动奖',style: TextStyle(fontSize: 12,color: Color(0XFFFFFFFF))),
                              ],
                            )
                          )
                        ],

                      ),
                    )
                  )

                ],)
                
              ],
            ),
          ),
          Positioned(child: buildTop()),
        ],
      )),
    ));
  }

  buildTop() {
    return Container(
      // color: Color(0xFFFFFFFF),
      child: QGNavigationBar(
          title: "抽奖活动",
          rightItem: Container(
            margin: EdgeInsets.only(right: 15.rpx),
            child: GestureDetector(
              onTap: (){
                Navigator.of(context).pushNamed(pageRecordPage);
              },
              child: Text('抽奖记录',
                style: TextStyle(fontSize: 16, color: Color(0XFFFFFFFF))),
            )
            
            ,
          )),
    );
  }
}

Future _loadVideoItem(String image) {
  Future Function(String) decoder;
  if (image.startsWith(RegExp(r'https?://'))) {
    decoder = SVGAParser.shared.decodeFromURL;
  } else {
    decoder = SVGAParser.shared.decodeFromAssets;
  }
  return decoder(image);
}
